package com.sphd.emanu.fragment.materialsclassify;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.materialsstorelist.MaterialsStoreListActivity;

import com.sphd.emanu.adapter.TMtCategoryAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.interfaces.Letter;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.utils.PermissionConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MaterialsclassifyFragment extends MVPBaseFragment<MaterialsclassifyContract.View, MaterialsclassifyPresenter> implements MaterialsclassifyContract.View {
    List<Category> data;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Unbinder unbinder;
    TMtCategoryAdapter tMtCategoryAdapter;
    Bundle bundle;
    Intent intent;
    User user;
    Category category;
    Letter letter;
    List<Moudle> moudles;
    boolean isCanDo;
    public void setLetter(Letter letter) {
        this.letter = letter;
    }

    public void setData(List<Category> data) {
        this.data = data;
        tMtCategoryAdapter.setNewData(data);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.myswf_and_recycler;
    }

    @Override
    protected void initView() {
        intent = new Intent();
        unbinder = ButterKnife.bind(this, rootView);
        initEvent();
        bundle = getArguments();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        data = (List<Category>) bundle.getSerializable(Constant.CATEGORY_LIST);
        category = (Category) bundle.getSerializable(Constant.CATEGORY);
        //初始化权限
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudles, PermissionConfig.MENU_STOREHOUSE_8);
        initAdapter();
        tMtCategoryAdapter.setNewData(data);
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(() -> mySwf.setRefreshing(false));
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                bundle.putSerializable(Constant.CATEGORY, (Category) adapter.getItem(position));
                intent.putExtras(bundle);
                intent.setClass(context, MaterialsStoreListActivity.class);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemLongClick(adapter, view, position);
                final Category category = (Category) adapter.getItem(position);
                if (!category.getIsLocked())
                    new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE).setTitleText("确定要删除此分类吗？").setConfirmText("确定")
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    mPresenter.deleteMaterialCategory(category.getId(), position, sweetAlertDialog);
                                }
                            }).setCancelText("取消").setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismiss();
                        }
                    }).show();
            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                final Category categoryThis = (Category) adapter.getItem(position);
                if (!categoryThis.getIsLocked()) {
                    View viewDialog = View.inflate(context, R.layout.dialog_edit, null);
                    final MaterialEditText myEdit = (MaterialEditText) viewDialog.findViewById(R.id.my_edit);
                    myEdit.setText(categoryThis.getName());
                    final AlertDialog alertDialog = getDialogBuilder(viewDialog, "编辑分类", "确定", "取消");
                    alertDialog.show();
                    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (TextUtils.isEmpty(myEdit.getText().toString().trim())) {
                                toast("分类不能为空");
                            } else {
                                mPresenter.updateMaterialCategory(categoryThis.getId(), 1, myEdit.getText().toString().trim(),
                                        user.getOid(), category.getId(), position,
                                        alertDialog);
                            }
                        }
                    });
                }
            }
        });
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecycler);
        tMtCategoryAdapter = new TMtCategoryAdapter(R.layout.item_tmt_category, new ArrayList<>(), isCanDo);
        if (null != tMtCategoryAdapter.getEmptyView()) {
            tMtCategoryAdapter.setEmptyView(emptyView);
        }
        myRecycler.setAdapter(tMtCategoryAdapter);
    }

    @Override
    protected void initData() {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void setDelRes(int res, int pos, SweetAlertDialog sweetAlertDialog) {
        if (res == 1) {
            toast("删除成功");
            tMtCategoryAdapter.remove(pos);
            sweetAlertDialog.dismiss();
        } else {
            toast("删除失败，请重试！");
        }
    }

    @Override
    public void setAddRes(int res, AlertDialog alertDialog) {
        if (res == 1) {
            toast("操作成功");
            alertDialog.dismiss();
            // TODO: 2017/8/22 操作成功回调 activity的方法
            letter.readyed();
        } else {
            toast("操作失败");
        }
    }
}
